package com.hpin.fansea.ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.LoginResult;
import com.hpin.fansea.bean.LoginUserInfo;
import com.hpin.fansea.bean.RegistInfo;
import com.hpin.fansea.utils.CommonUtils;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;
import com.lidroid.xutils.http.RequestParams;

/**
 *
 * 注册填写资料页面
 * Created by Aaron on 15/9/10.
 */
public class RegistInformationActivity extends BaseActivity {

    private TextView et_phone;
    private EditText et_nick;
    private EditText et_psw;
    private EditText et_againPsw;
    private EditText et_email;
    private EditText et_weibo;

    private Button btn_regist;

//    private Editor editor;

    private String nick;
    private String phone_num;
    private String code_num;
    private String psw_num;
    private String againPsw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_regist_information);

        mApplication.addActivity(this);

        // 获得手机号和验证码
        phone_num = getIntent().getStringExtra("phone_num");
        code_num = getIntent().getStringExtra("code_num");

        initView();
    }

    private void initView() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.registe));
        
//        tv_regist_community = (TextView) findViewById(R.id.tv_regist_community);
//        tv_regist_city = (TextView) findViewById(R.id.tv_regist_city);

        et_phone = (TextView) findViewById(R.id.et_phone);
        btn_regist = (Button) findViewById(R.id.btn_regist);

        et_nick = (EditText) findViewById(R.id.et_regist_nick);
        et_psw = (EditText) findViewById(R.id.et_regist_psw);
        et_againPsw = (EditText) findViewById(R.id.et_regist_againPsw);
        et_email = (EditText) findViewById(R.id.et_regist_email);
        et_weibo = (EditText) findViewById(R.id.et_regist_weibo);
//        //选择城市
//        ll_register_city = (LinearLayout) findViewById(R.id.ll_register_city);
//        //选择小区
//        ll_register_community = (LinearLayout) findViewById(R.id.ll_register_community);

        et_phone.setText(phone_num);

        iv_left.setOnClickListener(this);
        btn_regist.setOnClickListener(this);
//        ll_register_city.setOnClickListener(this);
//        ll_register_community.setOnClickListener(this);

    }

    /**
     * 注册验证
     */
    private void registValidate() {

        psw_num = et_psw.getText().toString().trim();
        againPsw = et_againPsw.getText().toString().trim();
        nick = et_nick.getText().toString().trim();

        String email = et_email.getText().toString().trim();
        String weibo = et_weibo.getText().toString().trim();
        
        if (CommonUtils.isNull(nick)) {
            Toast.makeText(this, "请填昵称", Toast.LENGTH_SHORT).show();
            return;
        }

        if (CommonUtils.isNull(psw_num)) {
            // 检测密码
            Toast.makeText(this, "请填写密码", Toast.LENGTH_SHORT).show();
            return;
        }

        if (CommonUtils.isNull(againPsw)) {
            // 检测重复密码为空
            Toast.makeText(this, "请填写重复密码", Toast.LENGTH_SHORT).show();
            return;
        }

        // 检测密码是否相等
        if (!psw_num.equals(againPsw)) {
            Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }

        if (psw_num.length()<6 || psw_num.length() > 16) {
            Toast.makeText(getApplicationContext(), "密码应在6-16位",
                    Toast.LENGTH_SHORT).show();
            return;
        }

//		//城市
//		String registerCity = tv_regist_city.getText().toString();
//
//		if (TextUtils.isEmpty(registerCity)) {
//			showToast("请选择城市!");
//			return;
//		}
//
//		//小区
//		String registerCommunity = tv_regist_community.getText().toString();
//
//		if (TextUtils.isEmpty(registerCommunity)) {
//			showToast("请选择小区!");
//			return;
//		}

        // 注册验证json
        RegistInfo registInfo = new RegistInfo();
        LoginUserInfo info = new LoginUserInfo();
        registInfo.lat= "";
        registInfo.lng = "";
        registInfo.systemType = Constant.SYSTEM_TYPE;
        registInfo.verifycode= code_num;

        info.userId= phone_num;
        info.userName= "";
        info.nickname = nick;
        info.password = CommonUtils.toMd5(psw_num.getBytes());
        info.email = email;
        info.weibo = weibo;

        JSONObject userJson = (JSONObject) JSONObject.toJSON(info);
        JSONObject registJson = (JSONObject) JSONObject.toJSON(registInfo);
        registJson.put("userInfo", userJson);
        String registInf = JSONObject.toJSONString(registJson);

        RequestParams params = new RequestParams();
        params.addBodyParameter("registInfo", registInf);

        HttpUtil.getInstance().setPostRequest("注册中...", this, Constant.WEB_ROOT + Constant.REGISTSERVICE,
                params, new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {
                        try {

                            LoginResult loginResult = JSONObject.parseObject(str, LoginResult.class);

                            if (Constant.STATUS_SUCESS.equals(loginResult.status)) {

                                showToast("注册成功");

                                //登陆成功 保存用户数据
                                SharedPreferences.Editor edit = sp.edit();

                                edit.putString(Constant.token, loginResult.token);
                                edit.putString(Constant.communityId, loginResult.userInfo.communityId);
                                edit.putString(Constant.userName, loginResult.userInfo.userName);
                                edit.putString(Constant.userId, loginResult.userInfo.userId);
                                edit.putString(Constant.nickname, loginResult.userInfo.nickname);
                                edit.putString(Constant.headPic, loginResult.userInfo.headPic);
                                edit.putString(Constant.address, loginResult.userInfo.address);
                                edit.putString(Constant.password, loginResult.userInfo.password);
                                edit.putString(Constant.status, loginResult.userInfo.status);
                                edit.putString(Constant.email, loginResult.userInfo.email);
                                edit.putString(Constant.mobile, loginResult.userInfo.mobile);
                                edit.putString(Constant.cityId, loginResult.userInfo.cityId);
                                edit.putString(Constant.projectName, loginResult.userInfo.projectName);
                                edit.putString(Constant.houseCode, loginResult.userInfo.houseCode);
                                edit.putString(Constant.currentCommunityId, loginResult.userInfo.currentCommunityId);
                                edit.putString(Constant.houseId, loginResult.userInfo.houseId);
                                edit.putString(Constant.gender, loginResult.userInfo.gender);
                                edit.putString(Constant.birthday, loginResult.userInfo.birthday);
                                edit.putString(Constant.age, loginResult.userInfo.age);
                                edit.putString(Constant.idNo , loginResult.userInfo.idNo);
                                edit.putString(Constant.cityName, loginResult.userInfo.cityName);
                                edit.putString(Constant.communityName, loginResult.userInfo.communityName);
                                edit.putString(Constant.buildingNo, loginResult.userInfo.buildingNo);
                                edit.putString(Constant.unitNo, loginResult.userInfo.unitNo);
                                edit.putString(Constant.houseNo, loginResult.userInfo.houseNo);

                                edit.commit();

//                         判断小区id为空
                                if (TextUtils.isEmpty(loginResult.userInfo.communityId)) {
                                    Intent intent = new Intent(RegistInformationActivity.this, FirstCitySelectActivty.class);
                                    startActivity(intent);
                                     finish();
                                } else {
                                    Intent intent = new Intent(RegistInformationActivity.this, HomeActivity.class);
                                    startActivity(intent);
                                     finish();
                                }

                            }else {
                                showToast(loginResult.message);
                            }

                        }catch (Exception e) {

                            e.printStackTrace();
                            showToast("登陆失败");
                        }
                    }
                }, null);

    }

//    private LinearLayout ll_register_city;
//    private LinearLayout ll_register_community;
//    private TextView tv_regist_community;
//    private TextView tv_regist_city;
//    private String cityId;
//    private String cityName;
//    private String communityId;
//    private String communityName;

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//
//        if (resultCode == 10) {//选择城市后返回回来的 resultCode
//            cityId = data.getStringExtra("cityId");
//            cityName = data.getStringExtra("cityName");
//            tv_regist_city.setText(cityName);
//        }
//        if (resultCode == 20) {//选择小区后返回回来的 resultCode
//            communityId = data.getStringExtra("communityId");
//            communityName = data.getStringExtra("communityName");
//            tv_regist_community.setText(communityName);
//        }
//
//    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {

            case R.id.btn_regist:  //注册

                registValidate();
                break;

            case R.id.iv_left://返回

                finish();
                break;

//            case R.id.ll_register_community://选择小区
//
//                if (cityId == null) {
//                    showToast("请先选择城市");
//                } else {
//                    Intent intent = new Intent(ctx, CommunityListActivity.class);
//                    Bundle bundle = new Bundle();
//                    bundle.putString("cityId", cityId);
//                    intent.putExtras(bundle);
//                    startActivityForResult(intent, 1);
//                }
//                break;
//            case R.id.ll_register_city://选择城市
//                //跳转选择城市
//                Intent intent = new Intent(ctx, CityListActivty.class);
//                startActivityForResult(intent, 0);
//
//                break;

            default:
                break;
        }
    }
}
